﻿using System;
using Abp.Application.Services.Dto;
using FutureApp.Navigation.Dto;
using Abp.Domain.Repositories;
using System.Linq;
using Abp.Auditing;

namespace FutureApp.Navigation
{
    /// <AppService/>
    [DisableAuditing]
    public class MenuAppService : IMenuAppService
    {
        /// <constructor/>
        public MenuAppService(IRepository<Menu, Guid> menuRepository)
        {
            this.MenuRepository = menuRepository;
        }

        private IRepository<Menu, Guid> MenuRepository { get; }

        /// <inheritDoc/>
        public ListResultDto<MenuEntityDto> GetMenus()
        {
            var menus = this.MenuRepository.GetAllList();
            return new ListResultDto<MenuEntityDto>(
                menus.Where(menu => menu.Parent == null)
                .OrderBy(menu => menu.Order)
                .Select(this.Transfer)
                .ToList()
            );
        }

        private MenuEntityDto Transfer(Menu menu)
        {
            return new MenuEntityDto
            {
                Code = menu.Code,
                Name = menu.Name,
                Icon = menu.Icon,
                Order = menu.Order,
                NavigationUrl = menu.NavigationUrl,
                RequiresAuthentication = menu.RequiresAuthentication,
                RequiredPermission = menu.RequiredPermission,
                Menus = menu.Children.Select(this.Transfer).ToArray()
            };
        }
    }
}
